package com.knowledgegraph.controller;

import com.knowledgegraph.dto.G6GraphData;
import com.knowledgegraph.service.G6DataConversionService;
import com.knowledgegraph.service.GraphService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/knowledge-graph")
@Tag(name = "Graph Visualization API", description = "图可视化和G6数据格式接口")
public class GraphVisualizationController {
    
    @Autowired
    private G6DataConversionService g6DataConversionService;
    
    @Autowired
    private GraphService graphService;
    
    @GetMapping("/g6/full-graph")
    @Operation(summary = "获取完整图数据", description = "获取G6格式的完整知识图谱数据")
    public ResponseEntity<G6GraphData> getFullGraphForG6() {
        G6GraphData graphData = g6DataConversionService.convertToG6Format();
        return ResponseEntity.ok(graphData);
    }
    
    @PostMapping("/g6/sub-graph")
    @Operation(summary = "获取子图数据", description = "根据节点ID列表获取G6格式的子图数据")
    public ResponseEntity<G6GraphData> getSubGraphForG6(
            @Parameter(description = "节点ID列表") @RequestBody List<Long> nodeIds) {
        G6GraphData graphData = g6DataConversionService.convertSubGraphToG6Format(nodeIds);
        return ResponseEntity.ok(graphData);
    }
    
    @GetMapping("/g6/tree/{rootNodeId}")
    @Operation(summary = "获取树状图数据", description = "以指定节点为根获取G6格式的树状图数据")
    public ResponseEntity<G6GraphData> getTreeForG6(
            @Parameter(description = "根节点ID") @PathVariable Long rootNodeId,
            @Parameter(description = "最大深度，默认为3") @RequestParam(defaultValue = "3") int maxDepth) {
        G6GraphData graphData = g6DataConversionService.convertTreeStructureToG6Format(rootNodeId, maxDepth);
        return ResponseEntity.ok(graphData);
    }
    
    @GetMapping("/analysis/shortest-path")
    @Operation(summary = "查找最短路径", description = "在两个节点间查找最短路径")
    public ResponseEntity<List<Map<String, Object>>> findShortestPath(
            @Parameter(description = "源节点ID") @RequestParam Long sourceNodeId,
            @Parameter(description = "目标节点ID") @RequestParam Long targetNodeId) {
        List<Map<String, Object>> path = graphService.findShortestPath(sourceNodeId, targetNodeId);
        return ResponseEntity.ok(path);
    }
    
    @GetMapping("/analysis/neighbors/{nodeId}")
    @Operation(summary = "查找邻居节点", description = "查找指定节点在给定深度内的所有邻居")
    public ResponseEntity<List<Map<String, Object>>> findNeighbors(
            @Parameter(description = "节点ID") @PathVariable Long nodeId,
            @Parameter(description = "搜索深度，默认为1") @RequestParam(defaultValue = "1") int depth) {
        List<Map<String, Object>> neighbors = graphService.findNeighbors(nodeId, depth);
        return ResponseEntity.ok(neighbors);
    }
    
    @GetMapping("/analysis/nodes-by-type")
    @Operation(summary = "按类型查找节点", description = "查找指定类型的所有节点")
    public ResponseEntity<List<Map<String, Object>>> findNodesByType(
            @Parameter(description = "节点类型") @RequestParam String nodeType) {
        List<Map<String, Object>> nodes = graphService.findNodesByType(nodeType);
        return ResponseEntity.ok(nodes);
    }
    
    @GetMapping("/analysis/statistics")
    @Operation(summary = "获取图统计信息", description = "获取图的基本统计信息")
    public ResponseEntity<Map<String, Long>> getGraphStatistics() {
        Map<String, Long> stats = graphService.getGraphStatistics();
        return ResponseEntity.ok(stats);
    }
    
    @GetMapping("/analysis/centrality/{nodeId}")
    @Operation(summary = "计算节点中心性", description = "计算指定节点的中心性指标")
    public ResponseEntity<Double> calculateNodeCentrality(
            @Parameter(description = "节点ID") @PathVariable Long nodeId) {
        Double centrality = graphService.calculateNodeCentrality(nodeId);
        return ResponseEntity.ok(centrality);
    }
    
    @GetMapping("/analysis/connected-components")
    @Operation(summary = "查找连通分量", description = "查找图中的所有连通分量")
    public ResponseEntity<List<Map<String, Object>>> findConnectedComponents() {
        List<Map<String, Object>> components = graphService.findConnectedComponents();
        return ResponseEntity.ok(components);
    }
    
    @GetMapping("/analysis/cycles")
    @Operation(summary = "检测环路", description = "在图中检测环路结构")
    public ResponseEntity<List<Map<String, Object>>> findCycles(
            @Parameter(description = "最大搜索深度，默认为5") @RequestParam(defaultValue = "5") int maxDepth) {
        List<Map<String, Object>> cycles = graphService.findCycles(maxDepth);
        return ResponseEntity.ok(cycles);
    }
}